package com.gopay.bis.cbrmb.masapay.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.cbrmb.masapay.operation.MasaPayHelper;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayBankDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayBankDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayEnrolmentCheckDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayEnrolmentCheckDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayPayerAuthValidateReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayPayerAuthValidateResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressListDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressListResDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.RefundReqDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.RefundResDTO;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.cbrmb.exchange.JacksonUtil;
import com.gopay.remote.bank.cbrmb.MasaPayRemoteService;

import masapay.uploadExpressList.UploadExpressDTO;
import masapay.uploadExpressList.UploadExpressListResDTO;

/**
 * 
 * @ClassName: MasaPayServiceImpl
 * @Description:
 * @author gd.cui
 * @date 2017年12月4日 上午10:40:26
 */
public class MasaPayServiceImpl implements MasaPayRemoteService {

	private static final Logger logger = LoggerFactory.getLogger(MasaPayServiceImpl.class);

	private MasaPayHelper masaPayHelper;

	@Override
	public MasaPayOrderDTOResp payOrder(MasaPayOrderDTOReq req) throws GopayException {
		logger.info(String.format("masapay pay bis-bank-cbrmb: %s", req.toString()));
		String result = masaPayHelper.httpPostPay(req);
		MasaPayOrderDTOResp resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, MasaPayOrderDTOResp.class);
			logger.info(String.format("masapay pay bis-bank-cbrmb resp: %s", resp.toString()));
		}
		return resp;
	}

	@Override
	public RefundResDTO refundOrder(RefundReqDTO refundReqDTO) throws GopayException {
		logger.info(String.format("masapay refudn bis-bank-cbrmb: %s", refundReqDTO.toString()));
		String result = masaPayHelper.httpPostRefund(refundReqDTO);
		RefundResDTO resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, RefundResDTO.class);
			logger.info(String.format("masapay pay bis-bank-cbrmb resp: %s", resp.toString()));
		}
		return resp;
	}

	@Override
	public MasaPayQueryOrderDTOResp queryOrder(MasaPayQueryOrderDTOReq req) throws GopayException {
		logger.info(String.format("masapay query bis-bank-cbrmb: %s", req.toString()));
		String result = masaPayHelper.httpPostQuery(req);
		MasaPayQueryOrderDTOResp resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, MasaPayQueryOrderDTOResp.class);
			logger.info(String.format("masapay query bis-bank-cbrmb resp: %s", resp.toString()));
		}
		return resp;
	}

	public void setMasaPayHelper(MasaPayHelper masaPayHelper) {
		this.masaPayHelper = masaPayHelper;
	}

	@Override
	public MasaPayUploadExpressListResDTO uploadExpress(MasaPayUploadExpressListDTO req) throws GopayException {
		logger.info(String.format("乾汇快递单上传参数: %s", req.toString()));

		// 调用乾汇快递单上传接口
		UploadExpressListResDTO result = masaPayHelper.doUploadExpress(req);

		MasaPayUploadExpressListResDTO resp = new MasaPayUploadExpressListResDTO();
		resp.setCharset(result.getCharset());
		resp.setErrCode(result.getErrCode());
		resp.setErrMsg(result.getErrMsg());
		resp.setExpressCount(result.getExpressCount());
		resp.setMerchantId(result.getMerchantId());
		resp.setLanguage(result.getLanguage());
		resp.setSignMsg(result.getSignMsg());
		resp.setSignType(result.getSignType());
		resp.setVersion(result.getVersion());

		List<MasaPayUploadExpressDTO> expList = new ArrayList<>();
		if (result.getUnsavelist() != null && result.getUnsavelist().length > 0) {
			for (UploadExpressDTO dto : result.getUnsavelist()) {
				MasaPayUploadExpressDTO exp = new MasaPayUploadExpressDTO();
				exp.setEtaDate(dto.getEtaDate());
				exp.setExpressCompany(dto.getExpressCompany());
				exp.setExpressDate(dto.getExpressDate());
				exp.setExpressNo(dto.getExpressNo());
				exp.setExpressSignMsg(dto.getExpressSignMsg());
				exp.setExpressUrl(dto.getExpressUrl());
				exp.setMerchantOrderNo(dto.getMerchantOrderNo());
				exp.setUploadDate(dto.getUploadDate());
				expList.add(exp);
			}

			resp.setUnsavelist(expList);
		}

		return resp;
	}

	@Override
	public MasaPayQueryRefundOrderDTOResp queryRefundOrder(MasaPayQueryRefundOrderDTOReq req) throws GopayException {
		logger.info(String.format("masapay refund query bis-bank-cbrmb: %s", req.toString()));
		String result = masaPayHelper.httpPostQueryRefund(req);
		MasaPayQueryRefundOrderDTOResp resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, MasaPayQueryRefundOrderDTOResp.class);
			logger.info(String.format("masapay query bis-bank-cbrmb resp: %s", resp.toString()));
		}
		return resp;
	}

	@Override
	public MasaPayBankDTOResp authenticationPayer(MasaPayBankDTOReq req) throws GopayException {
		logger.info(String.format("masapay authenticationPayer bis-bank-cbrmb: %s", req.toString()));
		String result = masaPayHelper.httpPostAuthenticationPayer(req);
		MasaPayBankDTOResp resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, MasaPayBankDTOResp.class);
			logger.info(String.format("masapay authenticationPayer bis-bank-cbrmb resp: %s", resp.toString()));
		}
		return resp;
	}

	@Override
	public MasaPayEnrolmentCheckDTOResp enrolmentCheck(MasaPayEnrolmentCheckDTOReq req) throws GopayException {
		logger.info(String.format("masapay enrolmentCheck bis-bank-cbrmb: %s", req.toString()));
		String result = masaPayHelper.httpPostEnrolmentCheck(req);
		MasaPayEnrolmentCheckDTOResp resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, MasaPayEnrolmentCheckDTOResp.class);
			logger.info(String.format("masapay enrolmentCheck bis-bank-cbrmb resp: %s", resp.toString()));
			logger.info(String.format("masapay enrolmentCheck bis-bank-cbrmb resp: %s", "bis-cbrmb end"));
		}
		return resp;
	}

	@Override
	public MasaPayPayerAuthValidateResp payerAuthValidate(MasaPayPayerAuthValidateReq req) throws GopayException {
		logger.info(String.format("masapay payerAuthValidate bis-bank-cbrmb: %s", req.toString()));
		String result = masaPayHelper.httpPostPayerAuthValidate(req);
		MasaPayPayerAuthValidateResp resp = null;
		if (result != null) {
			resp = JacksonUtil.readValue(result, MasaPayPayerAuthValidateResp.class);
			logger.info(String.format("masapay payerAuthValidate bis-bank-cbrmb resp: %s", resp.toString()));
		}
		return resp;
	}

}
